// pages/loginHome/index.js
var http = require('../../utils/http');
var api = require('../../utils/api');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        checked: false,
        showAgreementPopup: false, //隐私协议popup
        loading: false,
        phone: "",
        radio: '',
        if_vip: ""
    },
    jump1() {
        wx.showLoading({
            title: '打开中...',
        })
        wx.downloadFile({
            url: 'https://www.seemoreopt.com/api/readFiles/UserServe.pdf', //要预览的PDF的地址
            success: function (res) {
                console.log(res);
                if (res.statusCode === 200) { //成功
                    var Path = res.tempFilePath //返回的文件临时地址，用于后面打开本地预览所用
                    wx.openDocument({
                        filePath: Path, //要打开的文件路径
                        success: function (res) {
                            console.log('打开PDF成功');
                            wx.hideLoading()
                        }
                    })
                }
            },
            fail: function (res) {
                console.log(res); //失败
            }
        })
    },
    jump2() {
        wx.showLoading({
            title: '打开中...',
        })
        wx.downloadFile({
            url: 'https://www.seemoreopt.com/api/readFiles/ChildPrivacy.pdf', //要预览的PDF的地址
            success: function (res) {
                console.log(res);
                if (res.statusCode === 200) { //成功
                    var Path = res.tempFilePath //返回的文件临时地址，用于后面打开本地预览所用
                    wx.openDocument({
                        filePath: Path, //要打开的文件路径
                        success: function (res) {
                            console.log('打开PDF成功');
                            wx.hideLoading()
                        }
                    })
                }
            },
            fail: function (res) {
                console.log(res); //失败
            }
        })
    },
    jump3() {
        wx.showLoading({
            title: '打开中...',
        })
        wx.downloadFile({
            url: 'https://www.seemoreopt.com/api/readFiles/UserPrivacy.pdf', //要预览的PDF的地址
            success: function (res) {
                console.log(res);
                if (res.statusCode === 200) { //成功
                    var Path = res.tempFilePath //返回的文件临时地址，用于后面打开本地预览所用
                    wx.openDocument({
                        filePath: Path, //要打开的文件路径
                        success: function (res) {
                            console.log('打开PDF成功');
                            wx.hideLoading()
                        }
                    })
                }
            },
            fail: function (res) {
                console.log(res); //失败
            }
        })
    },
    callBackLogin(data) {
        let result = data.detail.result
        let res = data.detail.res
        this.setData({
            phone: res.data,
        })
        wx.setStorageSync('token', result.data.access_token)
        wx.setStorageSync('openid', result.data.user.openid)
        wx.setStorageSync('userInfo', result.data.user)
        if (this.data.radio == 2) {
            // 说明是普通用户  直接进入首页
            wx.switchTab({
                url: '/pages/index/index',
            })
        } else {
            if (result.data.user.telephone && result.data.user.if_vip) {
                if (result.data.child.length == 0) {
                    wx.navigateTo({
                        url: '/pages/activationInfo/index?phone=' + this.data.phone,
                    })
                } else {
                    wx.switchTab({
                        url: '/pages/index/index',
                    })
                }
            } else if (res.data && result.data.user.if_vip) {
                if (result.data.child.length == 0) {
                    wx.navigateTo({
                        url: '/pages/activationInfo/index?phone=' + this.data.phone,
                    })
                } else {
                    wx.switchTab({
                        url: '/pages/index/index',
                    })
                }
            }
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.hideHomeButton()
    },
    onChangeCheckBox(event) {
        this.setData({
            checked: event.detail,
        });
    },
    /** 点击用户隐私协议触发 */
    tapAgreementFun() {
        this.setData({
            showAgreementPopup: true
        })
    },
    iSeeQueryFun(e) {
        this.setData({
            checked: e.detail.checked,
            showAgreementPopup: e.detail.showAgreementPopup,
            loading:e.detail.loading
        });
    },
    handleLogin() {
        if (this.data.radio == '') {
            wx.showModal({
                showCancel: false,
                content: '请选择身份',
            })
            return
        }
        if (!this.data.checked) {
            // wx.showToast({
            //     icon: "success",
            //     image: '../../images/error.png',
            //     title: "请阅读隐私协议",
            //     mask: true
            // })
            this.setData({
                showAgreementPopup: true
            })
            return
        }
    },
    onChangeRadio(e) {
        console.log(e);
        this.setData({
            radio: e.detail
        })
        console.log(this.data.radio !== '');
    },

    /**
     * 微信一键登录
     */
    handleWxLogin(e) {
        if (this.data.radio == '') {
            wx.showModal({
                showCancel: false,
                content: '请选择身份',
            })
            return
        }
        this.setData({
            loading: true
        })
        wx.login({
            success: (res) => {
                const code = res.code;
                http.postHttp(api.wxlogin, {
                        code: code,
                        if_vip: this.data.radio == 1 ? true : false
                    },
                    (result) => {
                        if (result.code == 0) {
                            if (e.detail.errMsg == 'getPhoneNumber:ok') {
                                const {
                                    code
                                } = e.detail
                                http.postHttp(api.getUserPhone, {
                                        code: code,
                                        openid: result.data.user.openid
                                    },
                                    (res) => {
                                        if (res.code == 0) {
                                            this.setData({
                                                phone: res.data,
                                            })
                                            wx.setStorageSync('token', result.data.access_token)
                                            wx.setStorageSync('openid', result.data.user.openid)
                                            wx.setStorageSync('userInfo', result.data.user)
                                            if (this.data.radio == 2) {
                                                // 说明是普通用户  直接进入首页
                                                wx.switchTab({
                                                    url: '/pages/index/index',
                                                })
                                            } else {
                                                if (result.data.user.telephone && result.data.user.if_vip) {
                                                    if (result.data.child.length == 0) {
                                                        wx.navigateTo({
                                                            url: '/pages/activationInfo/index?phone=' + this.data.phone,
                                                        })
                                                    } else {
                                                        wx.switchTab({
                                                            url: '/pages/index/index',
                                                        })
                                                    }
                                                } else if (res.data && result.data.user.if_vip) {
                                                    if (result.data.child.length == 0) {
                                                        wx.navigateTo({
                                                            url: '/pages/activationInfo/index?phone=' + this.data.phone,
                                                        })
                                                    } else {
                                                        wx.switchTab({
                                                            url: '/pages/index/index',
                                                        })
                                                    }
                                                }
                                            }
                                        } else {
                                            wx.showModal({
                                                showCancel: false,
                                                content: res.message,
                                            })
                                        }
                                    },
                                    (err) => {
                                        console.log(err);
                                    }, {
                                        hideLoading: true
                                    }
                                )
                            } else {
                                wx.showModal({
                                    showCancel: false,
                                    content: "请允许获取手机号",
                                })
                                this.setData({
                                    loading: false
                                })
                            }

                        } else {
                            wx.showModal({
                                showCancel: false,
                                content: result.msg,
                            })
                        }
                    },
                    (err) => {
                        console.log(err);
                    }, {
                        hideLoading: true
                    }
                )
            },
        })
    },
    getPhoneFun(e) {
        if (e.detail.errMsg == 'getPhoneNumber:ok') {
            const {
                code
            } = e.detail
            http.postHttp(api.getUserPhone, {
                    code: code,
                    openid: this.data.openid
                },
                (res) => {
                    if (res.code == 0) {
                        this.setData({
                            phone: res.data,
                        })
                        this.handleWxLogin()
                    } else {
                        wx.showModal({
                            showCancel: false,
                            content: res.message,
                        })
                    }
                },
                (err) => {
                    console.log(err);
                }, {
                    hideLoading: true
                }
            )
        }

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})